package com.gzhryc.net_marketing.supplier.config.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.net_marketing.supplier.config.dao.SupplierLogisticsCostConfigDao;
import com.gzhryc.net_marketing.supplier.config.dao.db.SupplierLogisticsCompany;
import com.gzhryc.net_marketing.supplier.config.dao.db.SupplierLogisticsCostConfig;
import com.gzhryc.net_marketing.supplier.config.services.dto.SupplierLogisticsCostConfigSearch;
import com.gzhryc.shared_device.base.enums.ELogisticsAreaScope;
import com.gzhryc.shared_device.common.CountryAreaUtils;

public class SupplierLogisticsCostConfigService {

    static Logger log = Logger.getLogger(SupplierLogisticsCostConfigService.class);

    public static SupplierLogisticsCostConfigService self() {
        return MultiDBTools.getService(SupplierLogisticsCostConfigService.class, true);
    }

    public static SupplierLogisticsCostConfigService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, SupplierLogisticsCostConfigService.class, true);
    }

    SupplierLogisticsCostConfigDao dao;

    public SupplierLogisticsCostConfigService(String jdbcKey) {
        dao = new SupplierLogisticsCostConfigDao(jdbcKey);
    }

    public List<SupplierLogisticsCostConfig> findJoinBySearch(SupplierLogisticsCostConfigSearch search, Integer pageNo, Integer pageLimit) {
        Conditions condition = dao.buildConditions(search);
        condition.desc("createDate");

        try {
            return dao.findJoinByCondition(condition, pageNo, pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public Long countJoinBySearch(SupplierLogisticsCostConfigSearch search) {
        Conditions condition = dao.buildConditions(search);
        try {
            return dao.countJoin(condition);
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return 0L;
    }

    public List<SupplierLogisticsCostConfig> findJoinBySearch(SupplierLogisticsCostConfigSearch search) {
        Conditions condition = dao.buildConditions(search);
        condition.desc("createDate");

        try {
            return dao.findJoinByCondition(condition);
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public SupplierLogisticsCostConfig getById(Long id){
        try {
            return dao.getById(id);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public SupplierLogisticsCostConfig getJoinById(Long id){
        try {
            return dao.getJoinById(id);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    /**
     * 获取该物流公司全国物流费用
     * @param logisticsCompanyId
     * @return
     */
    public SupplierLogisticsCostConfig getCountryCost(String logisticsCompanyId){
        if(StringTools.isNotBlank(logisticsCompanyId)) {
            Conditions conditions = new Conditions(SupplierLogisticsCostConfig.class);
            conditions.and().is("logisticsCompanyId",logisticsCompanyId);
            conditions.and().is("areaScope", ELogisticsAreaScope.Country.index());

            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public SupplierLogisticsCostConfig getUsableConfig(String logisticsCompanyId,String province){
        SupplierLogisticsCostConfig result = null;
        if(StringTools.isNotBlank(logisticsCompanyId)){
            if(StringTools.isNotBlank(province)){
                Conditions conditions = new Conditions(SupplierLogisticsCostConfig.class);
                conditions.and().is("companyId",logisticsCompanyId);
                conditions.and().is("province",province);
                conditions.desc("createDate");
                try {
                    result = dao.getJoin(conditions);
                } catch (SQLException e) {
                    log.error(e.getMessage(),e);
                }
            }

            if(result == null){
                //取全国数据
                Conditions conditions = new Conditions(SupplierLogisticsCostConfig.class);
                conditions.and().is("companyId",logisticsCompanyId);
                conditions.and().is("areaScope",ELogisticsAreaScope.Country.index());
                conditions.desc("createDate");
                try {
                    result = dao.getJoin(conditions);
                } catch (SQLException e) {
                    log.error(e.getMessage(),e);
                }
            }
        }else if(StringTools.isNotBlank(province)){
            //不限定物流公司
            Conditions conditions = new Conditions(SupplierLogisticsCostConfig.class);
            conditions.and().is("province",province);
            conditions.desc("createDate");

            try {
                result = dao.getJoin(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }

        if(result == null){
            SupplierLogisticsCompany defaultLogisticsCompany = SupplierLogisticsCompanyService.self().getDefLogisticsCompany();
            if(defaultLogisticsCompany != null) {
                Conditions conditions = new Conditions(SupplierLogisticsCostConfig.class);
                conditions.and().is("companyId", defaultLogisticsCompany.getId());
                conditions.and().is("areaScope", ELogisticsAreaScope.Country.index());
                conditions.desc("createDate");

                try {
                    result = dao.getJoin(conditions);
                } catch (SQLException e) {
                    log.error(e.getMessage(), e);
                }
            }

            if(result == null){
                //如果默认没有全国，则取最新的一个全国配置
                Conditions conditions = new Conditions(SupplierLogisticsCostConfig.class);
                conditions.and().is("areaScope",ELogisticsAreaScope.Country.index());
                conditions.desc("createDate");

                try {
                    result = dao.getJoin(conditions);
                } catch (SQLException e) {
                    log.error(e.getMessage(),e);
                }
            }
        }
        return result;
    }

    public boolean add(SupplierLogisticsCostConfig logisticsCostConfig)throws LogicException {
        logisticsCostConfig.setCreateDate(new Date());
        if(CountryAreaUtils.getCountry() == null){
            CountryAreaUtils.initial();
        }
        if(CountryAreaUtils.getCountry() == null) {
            throw new LogicException("请先初始化中国城市地区");
        }
        logisticsCostConfig.setCountry(CountryAreaUtils.getCountry().getName());
        try {
            return dao.insert(logisticsCostConfig) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean updateNotNull(SupplierLogisticsCostConfig logisticsCostConfig)throws LogicException {
        try {
            return dao.updateNotNull(logisticsCostConfig) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteById(Long id) throws LogicException {
        try {
            return dao.deleteById(id) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteByCompanyId(String companyId) throws LogicException {
        if(StringTools.isNotBlank(companyId)) {
            Conditions conditions = new Conditions(SupplierLogisticsCostConfig.class);
            conditions.and().is("companyId",companyId);

            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

}
